package spring.cloud.study.entity.base;

import java.io.Serializable;
import java.text.MessageFormat;

import org.apache.commons.lang.StringUtils;

import lombok.Getter;
import lombok.Setter;
import spring.cloud.study.utils.GsonUtil;

public class ResponseModel implements Serializable {

	private static final long serialVersionUID = -3987058891400255687L;

	@Getter
	@Setter
	private String repCode;
	@Getter
	@Setter
	private String repMsg;
	@Getter
	@Setter
	private Object repData;

	public ResponseModel() {
		this.repCode = RespCodeEnum.SUCCESS.getCode();
	}

	public ResponseModel(RespCodeEnum repCodeEnum) {
		this.setRespCodeEnum(repCodeEnum);
	}

	// 成功
	public static ResponseModel success() {
		return ResponseModel.successMsg("成功");
	}

	// 自定义成功消息
	public static ResponseModel successMsg(String message) {
		ResponseModel responseModel = new ResponseModel();
		responseModel.setRepMsg(message);
		return responseModel;
	}

	// 自定义成功数据
	public static ResponseModel successData(Object data) {
		ResponseModel responseModel = new ResponseModel();
		responseModel.setRepCode(RespCodeEnum.SUCCESS.getCode());
		responseModel.setRepData(data);
		return responseModel;
	}

	// 失败
	public static ResponseModel errorMsg(RespCodeEnum respEnum) {
		ResponseModel responseModel = new ResponseModel();
		responseModel.setRespCodeEnum(respEnum);
		return responseModel;
	}

	/**
	 * 自定义处理错误消息
	 * 
	 * @param code    错误码
	 * @param message 错误消息
	 * @return
	 */
	public static ResponseModel errorMsg(String code, String message) {
		if (StringUtils.isBlank(code)) {
			return errorMsg(message);
		}

		ResponseModel responseModel = new ResponseModel();
		responseModel.setRepCode(code);
		responseModel.setRepMsg(message);
		return responseModel;
	}

	// 自定义失败消息
	public static ResponseModel errorMsg(String message) {
		ResponseModel responseModel = new ResponseModel();
		responseModel.setRepCode(RespCodeEnum.FAIL.getCode());
		responseModel.setRepMsg(message);
		return responseModel;
	}

	// 自定义code和失败信息
	public static ResponseModel errorMsg(RespCodeEnum repCodeEnum, String message) {
		ResponseModel responseModel = new ResponseModel();
		responseModel.setRepCode(repCodeEnum.getCode());
		responseModel.setRepMsg(message);
		return responseModel;
	}

	// 异常信息统一处理
	public static ResponseModel errorExceptionMsg(String message) {
		ResponseModel responseModel = new ResponseModel();
		responseModel.setRepCode(RespCodeEnum.EXCEPTION.getCode());
		responseModel.setRepMsg(RespCodeEnum.EXCEPTION.getDesc() + ": " + message);
		return responseModel;
	}

//	    @Override
//	    public String toString() {
//	        return ToStringBuilder.reflectionToString(this);
//	    }

	public String toJsonString() {
		// return JSONObject.toJSONString(this);
		return GsonUtil.toJsonDateFormat(this);
	}

//	public boolean isError() {
//		return !isSuccess();
//	}
//
//	public boolean isSuccess() {
//		if (this == null) {
//			return false;
//		}
//		return StringUtils.equals(this.repCode, RespCodeEnum.SUCCESS.getCode());
//	}

	public void setRespCodeEnum(RespCodeEnum repCodeEnum) {
		this.repCode = repCodeEnum.getCode();
		this.repMsg = repCodeEnum.getDesc();
	}

	public void setRespCodeEnum(RespCodeEnum repCodeEnum, Object... args) {
		this.repCode = repCodeEnum.getCode();
		this.repMsg = MessageFormat.format(repCodeEnum.getDesc(), args);
	}

}
